package com.jb.repair.acticity;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.jb.repair.R;
import com.jb.repair.common.AppConfig;
import com.jb.repair.dao.Db;
import com.jb.repair.model.SbbdzdyModel;
import com.jb.repair.model.SbzlModel;
import com.jb.repair.util.DownAttachmentDialogUtil;
import com.jb.repair.util.DownFileAsyncTask;
import com.jb.repair.util.IntentUtils;
import com.jb.repair.util.StringUtils;
import com.jb.repair.view.InfoTextView;
import com.jb.repair.view.TitleView;

import java.io.File;
import java.util.List;

import static android.widget.ActionMenuView.OnClickListener;

/**
 * 设备资料详情
 */
public class EquipInfoActivtity extends BaseActivity {

    private TitleView titleView;
    private InfoTextView tv_info, tv_name, tv_type, tv_version, tv_status, tv_remark, tv_attachment;
    private LinearLayout ll_sbzl;
    private Db db;
    private String fileName = "", typeName = "";

    private static final String TAG = "EquipInfoActivtity";
    private SbzlModel model;
    private int i = -1;
    private DownAttachmentDialogUtil dialogUtil;

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sbzlxq);
        initviews();
        db = Db.getInstance(this);
        // Log.i(model.toString(),);
        model = (SbzlModel) getIntent().getSerializableExtra("sbzl");
        String form_CODE = db.getName2("CODE", "TB_SYS_CODE", "CODE_NAME", "设备资料");
        // Log.e("1231", form_CODE);
        String id_zhuanye = getIntent().getStringExtra("id_zhuanye");

        String name1 = db.getName("CODE_NAME", "TB_SYS_CODE", "CODE", model.file_type_id);
        tv_info.setText(name1);
//        fileName = model.zlmc;
        tv_name.setText(model.file_name);
        //判断文件类型
        typeName = model.file_format_editable;
        if (typeName.equals("") || typeName == null) {
            if (fileName != null && !fileName.equals("")) {

                int index = fileName.lastIndexOf(".");
                if (index != -1) {
                    typeName = fileName.substring(index + 1).trim();
                } else {
                    typeName = "未知类型";
                }
            }
        }
        tv_type.setText(typeName);

        tv_status.setText(model.file_status);

        tv_remark.setText(model.note);

        String visible1 = db.getVisible(form_CODE, "file_version_editable");
        if (!visible1.equals("T")) {
            tv_version.setVisibility(View.GONE);
        }
        tv_version.setText(model.file_version_editable);
        //  Log.e("tv_attachment",model.FJID);
        Log.i("SSSA", model.fjid + "------2222");
        String name2 = db.getName("WJMC", "TB_FILE_LB", "GUID", model.fjid);

        Log.i("SSSA", "SSSSSSSSSSSSS" + name2);
        if (!StringUtils.isNullOrEmpty(name2)) {
            tv_attachment.setText(name2);
            i = 1;
        } else {
            String name4 = db.getName("WJMC", "TB_FILE_LB", "OBJ_ID", model.fjid);
            Log.i("SSSA", "SSSSSSSSSSSSS2" + name4);
            tv_attachment.setText(name4);
            i = 2;
        }


        //----------------------------------------------处理自定义字段逻辑-----------------------------------------------------------
        //查找自定义字段显示
        //拿到表单编码
        List<SbbdzdyModel> sbbdzdyModelList = db.getSbbdzdyModel(form_CODE, id_zhuanye);
        for (SbbdzdyModel model1 : sbbdzdyModelList) {
            if (model1.is_show_eqbook.equals("T")) {
                InfoTextView infoTextView = new InfoTextView(this);
                ll_sbzl.addView(infoTextView);
                //需要设置key的值
                infoTextView.setKey(model1.cus_name);
                //设置value的值

                String format = model1.field_format;
                String value = db.getZdyzdValue(model1.guid, model.guid);
                if (value == null) {
                    value = "";
                } else if (format != null && "时间".equals(format) && value.length() > 10) {
                    value = value.substring(0, 10);
                }
                infoTextView.setText(value);

//                String zdyzdValue = db.getZdyzdValue(model1.guid, model.guid);
//                infoTextView.setText(zdyzdValue);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.topMargin = (int) getResources().getDimension(R.dimen.ledger_info_textsize);
                infoTextView.setLayoutParams(params);
            }

        }
//-------------------------------------------------------------------------------------------------------

        //fillFakeData();
    }

    private void initviews() {
        titleView = (TitleView) this.findViewById(R.id.titleView);
        tv_info = (InfoTextView) this.findViewById(R.id.tv_info);
        tv_name = (InfoTextView) this.findViewById(R.id.tv_name);
        tv_type = (InfoTextView) this.findViewById(R.id.tv_type);
        tv_version = (InfoTextView) this.findViewById(R.id.tv_version);
        tv_status = (InfoTextView) this.findViewById(R.id.tv_status);
        tv_remark = (InfoTextView) this.findViewById(R.id.tv_remark);
        tv_attachment = (InfoTextView) this.findViewById(R.id.tv_attachment);
        ll_sbzl = (LinearLayout) findViewById(R.id.ll_sbzl);

        titleView.setOnClickListener(new TitleView.OnTitleViewClickListener() {
            @Override
            public void onLeftImageClick(View view) {
                super.onLeftImageClick(view);
                finish();
            }
        });

        tv_attachment.setOnClickListener(new OnClickListener(
        ) {
            public void onClick(View view) {

                Log.i("点击测试", "收到点击事件");
                //附件的guid
                final String guid;
                //附件的文件类型
                final String fjlx;
                if (i == 1) {
                    guid = db.getName("GUID", "TB_FILE_LB", "GUID", model.fjid);
                    fjlx = db.getName("LX", "TB_FILE_LB", "GUID", model.fjid);
                } else {
                    guid = db.getName("GUID", "TB_FILE_LB", "OBJ_ID", model.fjid);
                    fjlx = db.getName("LX", "TB_FILE_LB", "OBJ_ID", model.fjid);
                }
                //判断是否有附件
                boolean nullOrEmpty = StringUtils.isNullOrEmpty(tv_attachment.getText());
                Log.e("LLL", String.valueOf(nullOrEmpty));
                if (nullOrEmpty) {
                    return;
                } else {
                    //有附件
                    String filePath = AppConfig.ATTACHMENT_PATh + guid + "." + fjlx;
                    Log.e("1111", filePath);
                    File file = new File(filePath);
                    if (!file.exists()) {
                        Toast.makeText(EquipInfoActivtity.this, "文件不存在", Toast.LENGTH_SHORT).show();
                        dialogUtil = new DownAttachmentDialogUtil(EquipInfoActivtity.this, R.style.myDialog,
                                new DownAttachmentDialogUtil.AttachDialogListener() {
                                    @Override
                                    public void onClick(View v) {
                                        switch (v.getId()) {
                                            case R.id.attach_dialog_canlebtn:
                                                dialogUtil.dismiss();
                                                break;
                                            case R.id.attach_dialog_surebtn:
                                                new DownFileAsyncTask(EquipInfoActivtity.this, guid, "", fjlx).execute();
                                                dialogUtil.dismiss();

                                                break;
                                        }
                                    }

                                });
                        Window win = dialogUtil.getWindow();
                        win.setGravity(Gravity.CENTER);
                        dialogUtil.show();
                    } else {
                        IntentUtils.JumpToIntent(EquipInfoActivtity.this, fjlx, filePath);

                    }


                }

            }
        });
    }

    /**
     * 填充假数据
     */
    private void fillFakeData() {
        Log.i(TAG, "文件名称为：" + fileName);
        Log.i(TAG, "类型名称为：" + typeName);
        if (fileName.equals("设计图")) {
            tv_attachment.setText("某电厂300MW机组全面性热力系统图.dwg");
        } else if (fileName.equals("施工图")) {
            tv_attachment.setText("四段抽汽管道.dwg");
        } else if (fileName.equals("电气一次接线图")) {
            tv_attachment.setText("2#机组锅炉PC B段母线.dwg");
        } else if (fileName.equals("电气二次接线图")) {
            tv_attachment.setText("330KV.dwg");
        } else if (fileName.equals("技术说明书")) {
            tv_attachment.setText("WDZ-491技术说明书.doc");
        } else {
            tv_attachment.setText("无附件");
        }


    }
}
